# coding=utf-8

import threading
from conf.constant import *
from views.view_base import *
from common.middlewares import *
from controller.batch_controller import BatchController

class BatchView(ViewBase):
    def __init__(self):
        super(BatchView, self).__init__()

    @dispatch('/get_files_list', methods=['GET', 'POST'])
    @authenticated()
    def get_files_list(self):
        """从磁盘读取文件列表(新建任务弹出窗用)"""
        try:
            controller = BatchController()
            data = controller.get_files_list()
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data=None, msg="系统异常")

    @dispatch('/add', methods=['POST'])
    @authenticated()
    @verify_params(sub_set_id="int", file_name="string", remark="string", srid="int")
    def add_batch(self):
        try:
            sub_set_id = self.params.get('sub_set_id')  # 数据子集id
            file_name = self.params.get('file_name')
            remark = self.params.get('remark')
            srid = self.params.get('srid') or GIS_SRID_GCJ02
            user = self.user

            controller = BatchController()
            t = threading.Thread(target=controller.add_batch,
                                 args=(user.user_id, sub_set_id, file_name, remark, srid), name='线程:' + file_name)
            t.start()

            return self._render.render_json(code=RESPONSE_SUCCESS, data={}, msg="添加中")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data=None, msg="系统异常")

    @dispatch('/remove', methods=['POST'])
    @authenticated()
    @verify_params(batch_id="int")
    def remove_batch(self):
        try:
            batch_id = self.params.get('batch_id')
            # user = self.user

            controller = BatchController()
            t = threading.Thread(target=controller.remove_batch,
                                 args=(batch_id,), name='删除批次')
            t.start()

            return self._render.render_json(code=RESPONSE_SUCCESS, data={}, msg="删除成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data=None, msg="系统异常")

    @dispatch('/get_upload_progressbar', methods=['GET', 'POST'])
    @authenticated()
    @verify_params(file_ids='list')
    def get_upload_progressbar(self):
        """获取进度条"""
        try:
            file_ids = self.params.get('file_ids')
            controller = BatchController()
            data = controller.get_upload_progressbar(file_ids)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")

    @dispatch('/get', methods=['GET', 'POST'])
    @authenticated()
    @verify_params(batch_id='int')
    def get(self):
        """批次详情"""
        try:
            batch_id = self.params.get('batch_id')
            controller = BatchController()
            data = controller.get(batch_id)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")

    @dispatch('/get_list', methods=['GET', 'POST'])
    @authenticated()
    @verify_params(page_size='int', page_index='int', data_set_id='int')
    def get_list(self):
        """批次列表页"""
        try:
            user = self.user
            page_size = self.params.get('page_size')
            page_index = self.params.get('page_index')
            data_set_id = self.params.get('data_set_id')
            controller = BatchController()
            data = controller.get_list(data_set_id, user.user_id, page_size, page_index)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")


    # @dispatch('/get_task_list', methods=['GET', 'POST'])
    # @authenticated()
    # @verify_params(batch_id='int')
    # def get_task_list(self):
    #     """任务列表页"""
    #     try:
    #         user = self.user
    #         page_size = self.params.get('page_size') or 5
    #         page_index = self.params.get('page_index') or 1
    #         batch_id = self.params.get('batch_id')
    #
    #         # task_status_list = [TASK_STATUS_REJECTED]
    #         task_status_list = [TASK_STATUS_NEW, TASK_STATUS_SUBMITTED,
    #                             TASK_STATUS_CANCELED, TASK_STATUS_AUDITED,
    #                             TASK_STATUS_REJECTED]
    #         controller = BatchController()
    #         data = controller.get_task_list(batch_id, user.user_id, page_size, page_index, task_status_list)
    #         return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
    #     except:
    #         writelog(traceback.format_exc())
    #         return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")


    @dispatch('/get_data_list', methods=['POST'])
    @verify_params(page_size='int', page_index='int', batch_id='int')
    @authenticated()
    def get_data_list(self):
        """获取批次gis数据列表"""
        try:
            page_size = self.params.get('page_size')
            page_index = self.params.get('page_index')
            batch_id = self.params.get('batch_id')
            geo_layers = self.params.get('geo_layers')
            is_modified = self.params.get('is_modified')

            controller = BatchController()
            data = controller.get_data_list(batch_id, page_size, page_index, geo_layers, is_modified)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg='查询成功')
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data=None, msg='系统异常')


    @dispatch('/get_real', methods=['POST'])
    @verify_params(geo_layers='list', batch_id='int')
    @authenticated()
    def get_real(self):
        try:
            batch_id = self.params.get('batch_id')
            x_min = self.params.get('x_min')
            x_max = self.params.get('x_max')
            y_min = self.params.get('y_min')
            y_max = self.params.get('y_max')
            geo_layers = self.params.get('geo_layers') or [GEO_LAYER_BRIDGE, GEO_LAYER_BUILDING, GEO_LAYER_CULVERT,
                                                           GEO_LAYER_DECELERATION,
                                                           GEO_LAYER_DELINEATER, GEO_LAYER_GUARDBAR,
                                                           GEO_LAYER_ILLUMINATION, GEO_LAYER_LANDUSE,
                                                           GEO_LAYER_ROADLINE, GEO_LAYER_ROADPOLYGON,
                                                           GEO_LAYER_ROADSIGN, GEO_LAYER_TREELAWN, GEO_LAYER_TUNNEL,
                                                           GEO_LAYER_WATER]
            controller = BatchController()
            array = controller.get_real(batch_id, geo_layers, x_min, y_min, x_max, y_max)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=array, msg='查询成功')
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data=None, msg='系统异常')



